{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting jieba\n",
      "  Downloading https://files.pythonhosted.org/packages/c6/cb/18eeb235f833b726522d7ebed54f2278ce28ba9438e3135ab0278d9792a2/jieba-0.42.1.tar.gz (19.2MB)\n",
      "Building wheels for collected packages: jieba\n",
      "  Running setup.py bdist_wheel for jieba: started\n",
      "  Running setup.py bdist_wheel for jieba: finished with status 'done'\n",
      "  Stored in directory: C:\\Users\\Administrator\\AppData\\Local\\pip\\Cache\\wheels\\af\\e4\\8e\\5fdd61a6b45032936b8f9ae2044ab33e61577950ce8e0dec29\n",
      "Successfully built jieba\n",
      "Installing collected packages: jieba\n",
      "Successfully installed jieba-0.42.1\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "twisted 18.7.0 requires PyHamcrest>=1.9.0, which is not installed.\n",
      "You are using pip version 10.0.1, however version 24.0 is available.\n",
      "You should consider upgrading via the 'python -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install jieba"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#词云\n",
    "import jieba\n",
    "from wordcloud import WordCloud\n",
    "import matplotlib.pyplot as plt\n",
    "import imageio\n",
    "f=open('data/mess.txt',encoding='utf-8')\n",
    "t=f.read()\n",
    "f.close()\n",
    "words=jieba.lcut()\n",
    "txt=''.join(words)\n",
    "image=imageio.imread('data/horse.png')\n",
    "w=WordCloud(font_path='data/FZFSJW.TTF',background_color='white',mask=image)\n",
    "w.generate(txt)\n",
    "w.to_file('data/test.png')\n",
    "plt.imshow(w,interpolation='bilinear')\n",
    "plt.axis(\"off\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#项目实施4中文文本自动文摘\n",
    "import re\n",
    "import jieba.analyse\n",
    "import jieba.posseg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_sentence(text):\n",
    "    split_num=0\n",
    "    sentences=[]\n",
    "    for paragraph in text.split(\"\\n\"):\n",
    "        if paragraph:\n",
    "            sentence_num=0\n",
    "            for paragraph in re.split(\"!|.|?\",paragraph):\n",
    "                if paragraph:\n",
    "                    mark=[]\n",
    "                    if split_num==0\n",
    "                        mark.append(\"FIRSTSECTION\")\n",
    "                    if sentence_num==0:\n",
    "                        mark.append(\"FIRSTSENTENCE\")\n",
    "                    sentence.append({\"text\":paragraph,\"pos\":\n",
    "                                    {\"x\":split_num,\"y\":sentence_num,\"mark\":mark}})\n",
    "                    sentence_num=sentence_num+1\n",
    "            split_num=split_num+1\n",
    "            sentences[-1][\"pos\"][\"mark\"].append(\"LASTSENTENCE\")\n",
    "    for i in range(0,len(sentense)):\n",
    "        if sentences[i][\"pos\"][\"x\"]==sentences[-1][\"pos\"][\"x\"]:\n",
    "            sentences[i][\"pos\"][\"mark\"].append(\"LASTSECTION\")\n",
    "return sentences"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample=open('mess.txt',encoding='utf-8').read()\n",
    "sentences=get_sentence(sample)\n",
    "print(\"对文本进行分词:\\n\",sentences[:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_keywords(text):\n",
    "    keywords=jieba.analyse.extract_tags(text,topk=100,withWeight=False,allowPOS=('n','vn','v'))\n",
    "    return keywords\n",
    "keywords=get_keywords(sample)\n",
    "print(\"对文本进行关键词提取:\\n\",keywords[:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_weight(sentences,keywords):\n",
    "    for sentencs in sentences:\n",
    "        mark=sentence[\"pos\"][\"mark\"]\n",
    "        weightPos=0\n",
    "        if \"FIRSTSECTION\" in mark:\n",
    "            weightPos=weightPos+2\n",
    "        if \"FIRSTSETENCE\" in mark:\n",
    "            weightPos=weightPos+2\n",
    "        if \"LASTSENTENCE\" in mark:\n",
    "            weightPos=weightPos+1\n",
    "        if \"LASTSECTION\" in mark:\n",
    "            weightPos=weightPos+1\n",
    "        sentence[\"weightPos\"weightPos\n",
    "    index=[\"冬梦\",\"飞跃\"]\n",
    "    for sentece in sentences:\n",
    "                 sentence[\"weightCueWords\"]=0\n",
    "                 sentence[\"weightKeywords\"]=0\n",
    "    for i in index:\n",
    "                 for sentence in sentences:\n",
    "                     if sentence[\"text\"].find(i)>=0:\n",
    "                        sentence[\"weightCueWords\"]=1\n",
    "    for keyword in keywords:\n",
    "                 for sentence in sentences:\n",
    "                     if sentence[\"text\"].find(keyword)>=0:\n",
    "                         sentence[\"weightKeywords\"]=sentence[\"weightKeywords\"]+1\n",
    "    weight=[]\n",
    "    for sentence in sentences:\n",
    "                 sentence[\"weight\"]=sentence[\"weightPos\"]+2*\n",
    "                 sentence[\"weightCueWords\"]+sentence[\"weightKeywords\"]\n",
    "                 weight.append(sentence[\"wieght\"])\n",
    "    return weight"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_sum(sentences,keywords,ratio=0.1):\n",
    "    summary=list()\n",
    "    sentences=sorted(sentences,key=lambda k:k['weight'],reverse=True)\n",
    "    length=0\n",
    "    for i in range(len(sentences)):\n",
    "        if length<3:\n",
    "            if i<ratio*len(sentences):\n",
    "                sentence=sentences[i]\n",
    "                summary.append(sentence[\"text\"])\n",
    "                length+=1\n",
    "    return summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "abstract_content=\"\\n\".join(get_sum(sentences,keywords))\n",
    "print('生成自动文摘:\\n',abstract_content)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
